<template>
	<view class="container">
		<uni-section :title="sectionTitle" type="line">
			<uni-row class="db" :gutter="gutter" :width="nvueWidth">
				<uni-col :span="2"><view class="num-text" style="text-align: right;">第</view></uni-col>
				<uni-col :span="6"><uni-number-box :disabled="!editableSign" :min="1" :max="maxStage" v-model="newStage" @change="changeStage" /></uni-col>
				<uni-col :span="15"><view class="num-text" style="text-align: left;">阶段</view></uni-col>
			</uni-row>
			<view class="content" style="font-size: 14px;">签到开始时间</view>
			<uni-datetime-picker :clear-icon="true" :border="false" :disabled="!editableSign" type="datetime" v-model="datetimeStartSign" @change="changeBeginTime" />
			<view class="content" style="font-size: 14px;">签到结束时间</view>
			<uni-datetime-picker :clear-icon="true" :border="false" :disabled="!editableSign" type="datetime" v-model="datetimeEndSign" @change="changeEndTime" />
			<view class="content" style="font-size: 14px;">签到提醒时间 (可选)</view>
			<uni-datetime-picker :clear-icon="true" :border="false" :disabled="!editableSign" type="datetime" v-model="sendMessageTime" />
			<view style="padding: 10px;text-align: right;">
				<button type="primary" plain="true" size="mini" @click="submit">{{ btnTextSign }}</button>
			</view>
			<view style="padding: 10px;text-align: right;">
				<button type="primary" plain="true" size="mini" @click="del">删除</button>
			</view>
		</uni-section>
	</view>
</template>

<script>
export default {
	data() {
		return {
			edit: false,
			stage: 1,
			count: 1,
			activityId: 1,
			activityName: '',
			maxStage: 1,
			newStage: 1,
			datetimeStartSign: '',
			datetimeEndSign: '',
			editableSign: false,
			btnTextSign: '编辑',
			changedSign: false,
			sectionTitle: '',
			sendMessageTime: '',

			apiUrl: 'http://localhost:8080/',
			gutter: 0,
			nvueWidth: 730
		};
	},
	onLoad: function(option) {
		this.activityId = option.activityId;
		this.activityName = option.activityName;
		this.maxStage = option.maxStage;
		if (option.edit == 'true') {
			this.edit = true;
			this.stage = option.stage;
			this.count = option.count;
			
			this.newStage = option.stage;
			this.datetimeStartSign = option.beginTime;
			this.datetimeEndSign = option.endTime;
			
			this.sectionTitle = '编辑 第'+this.stage+'阶段 第'+this.count+'次签到';
		}
		else {
			this.sectionTitle = '发布新签到';
		}
		uni.setNavigationBarTitle({
			title: this.activityName
		});
	},
	methods: {
		changeStage() {
			this.changedSign = true;
		},
		changeBeginTime(value) {
			this.changedSign = true;
		},
		changeEndTime(value) {
			this.changedSign = true;
		},
		del() {
			uni.request({
				method: 'DELETE',
				url: this.apiUrl + 'sign',
				header: {
					// Authorization: uni.getStorageSync('token')
				},
				data: {
					activityId: this.activityId,
					count: this.count,
					stage: this.stage
				},
				success: res => {
					console.log(res.data);
					if (res.statusCode == 200) {
						uni.showToast({
							icon: 'success',
							title: '签到删除成功'
						});
						uni.navigateBack({
							delta: 2
						});
					}
					if (res.statusCode == 400) {
						uni.showToast({
							icon: 'error',
							title: JSON.parse(res.data).message
						});
					}
				},
				fail: err => {
					reject(err);
					return uni.showToast({
						icon: 'loading',
						title: '请求失败'
					});
				}
			});
		},
		submit() {
			if (this.btnTextSign == '编辑') {
				this.editableSign = true;
				this.btnTextSign = '确认';
			} else if (this.datetimeEndSign == '' || this.datetimeStartSign == '') {
				uni.showToast({
					title: '时间不可为空！',
					icon: 'error',
					duration: 2000
				});
			} else if (this.datetimeEndSign <= this.datetimeStartSign) {
				uni.showToast({
					title: '结束时间不可早于开始时间！',
					icon: 'error',
					duration: 2000
				});
			} else if(this.sendMessageTime >= this.datetimeStartSign && this.sendMessageTime!=''){
				uni.showToast({
					title: '提醒时间不可晚于开始时间！',
					icon: 'error',
					duration: 2000
				});	
			} else{
				if (this.datetimeStartSign.length == 11) {
					// 统一前后端时间格式
					this.datetimeStartSign += '00:00:00';
				}
				if (this.datetimeEndSign.length == 11) {
					// 统一前后端时间格式
					this.datetimeEndSign += '23:59:59';
				}
				this.editableSign = false;
				this.btnTextSign = '编辑';
				if (this.changedSign) {
					if (this.edit == false) {
						uni.request({
							method: 'POST',
							url: this.apiUrl + 'sign',
							header: {
								// Authorization: uni.getStorageSync('token')
							},
							data: {
								activityId: this.activityId,
								beginTime: this.datetimeStartSign,
								endTime: this.datetimeEndSign,
								stage: this.newStage,
								sendMessageTime: this.sendMessageTime
							},
							success: res => {
								console.log(res.data);
								if (res.statusCode == 200) {
									uni.showToast({
										icon: 'success',
										title: '发布成功'
									});
								}
								if (res.statusCode == 400) {
									uni.showToast({
										icon: 'error',
										title: JSON.parse(res.data).message
									});
								}
							},
							fail: err => {
								reject(err);
								return uni.showToast({
									icon: 'loading',
									title: '请求失败'
								});
							}
						});
					} else {
						uni.request({
							method: 'PUT',
							url: this.apiUrl + 'sign',
							header: {
								// Authorization: uni.getStorageSync('token')
							},
							data: [
								{
									activityId: this.activityId,
									beginTime: this.datetimeStartSign,
									count: this.count,
									endTime: this.datetimeEndSign,
									stage: this.stage
								},
								{
									activityId: this.activityId,
									beginTime: this.datetimeStartSign,
									endTime: this.datetimeEndSign,
									stage: this.newStage
								}
							],
							success: res => {
								console.log(res.data);
								if (res.statusCode == 200) {
									this.count = res.data;
									this.stage = this.newStage;
									uni.showToast({
										icon: 'success',
										title: '修改成功'
									});
								}
								if (res.statusCode == 400) {
									uni.showToast({
										icon: 'error',
										title: JSON.parse(res.data).message
									});
								}
							},
							fail: err => {
								reject(err);
								return uni.showToast({
									icon: 'loading',
									title: '请求失败'
								});
							}
						});
					}
				}
				this.changedSign = false;
			}
		}
	}
};
</script>

<style>
.container {
	padding: 20px;
	font-size: 14px;
	line-height: 24px;
}
.content {
	padding: 15px;
}
</style>
